/************************************************************************

							Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  							   cached_ptr.h

	$Header: /resource_editor/string_vector.h $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "string_vector.h"

#include "streambuf_operators.h"

char const t_resource_traits<t_string_vector>::prefix[]    = "text";

char const t_resource_traits<t_string_vector>::extension[] = "txt";



// ----------------------------------------------------------------------------
// read a string vector from a resource file
// ----------------------------------------------------------------------------
bool read( std::streambuf& buffer, t_string_vector& text )
{
	unsigned short line_count;
	unsigned short string_length;
	int            i;

	buffer >> line_count;
	text.resize( line_count );
	for (i = 0; i < line_count; i++)
	{
		buffer >> string_length;
		text[i].resize( string_length );
		if (string_length > 0 && buffer.sgetn( &(*text[i].begin()), string_length ) != string_length)
			return false;
	}
	return true;
}

// ---------------------------------------------------------------------------
// write data to a file
// ---------------------------------------------------------------------------
bool write( std::streambuf& buffer, t_string_vector const& text )
{
	unsigned short line_count;
	unsigned short string_length;
	int            i;

	line_count = text.size();
	buffer << line_count;
	for (i = 0; i < line_count; i++)
	{
		string_length = text[i].length();
		buffer << string_length;
		buffer.sputn( &(*text[i].begin()), string_length );
	}
	return true;
}

// ---------------------------------------------------------------------------
// read a string vector from a resource file
// ---------------------------------------------------------------------------
bool read( std::streambuf& buffer, std::string& text )
{
	t_uint32 size = get< t_uint32 >( buffer );
	
	text.resize( size );
	
	std::string::iterator str_iter;
	
	for ( str_iter = text.begin(); str_iter != text.end(); ++str_iter )
	{
		*str_iter = get<std::string::value_type> (buffer);
	}

	return true;
}

// ---------------------------------------------------------------------------
// write data to a file
// ---------------------------------------------------------------------------
bool write( std::streambuf& buffer, std::string const& text )
{
	put< t_uint32 >( buffer, text.size() );
	
	std::string::const_iterator str_iter;
	
	for ( str_iter = text.begin(); str_iter != text.end(); ++str_iter )
	{
		put<std::string::value_type> (buffer, *str_iter);
	}
	
	return true;
}
